$(function() {
    //functions
    function getOriginalFromThumb(imagePath) {
        return imagePath.replace('-150x150', '');
    }
    //
//events binding
    $('a').live('click', function(event) {
        if ($(this).attr('href') === '#') {
            event.preventDefault();
        }
    });


    $('a.load-chosen').live('click', function(event) {
        event.preventDefault();
        var countDivs = $('.sectionOneNewBox').length;
        var allSelf = $('a.load-chosen');
        var self = $(this);
        var sec = self.attr('sec');
        var loadMore = $('#load-more');
        loadMore.attr('sec', sec);
        var isProvince;
        var action;
        if (self.hasClass('provinces')) {
            isProvince = true;
            action = 'loadProvince';
            loadMore.addClass('provinces');
        } else {
            isProvince = false;
            action = 'loadSection';
        }
        if (self.hasClass('seeMoreFromSection')) {

        } else {
            loadMore.attr('offset', 0);
            loadMore.addClass('load-chosen');
            loadMore.removeClass('noMore');
            loadMore.html('شاهد المزيد');
            $('#section-content').html('');
        }
        if (countDivs < 6) {
            var offset = (typeof loadMore.attr('offset') === 'undefined') ? 100000000 : loadMore.attr('offset');
        } else {
            var offset = (typeof loadMore.attr('offset') === 'undefined') ? 0 : loadMore.attr('offset');
        }
        if (action === 'loadProvince') {
            var prov = $('#cur-prov').val();
            var sendData = {
                action: action,
                section: sec,
                offset: offset,
                province: prov
            };
        } else {
            var sendData = {
                action: action,
                section: sec,
                offset: offset
            };
        }
        $.ajax({
            url: phpObj.ajaxPath,
            dataType: 'json',
            data: sendData,
            beforeSend: function() {
                $('.section-act.active').removeClass('active');
            },
            success: function(res) {
                var section, videos, albums;
                if (action === 'loadProvince') {
                    section = res.section;
                    videos = res.videos;
                    albums = res.albums;
                } else {
                    section = res;
                    videos = false;
                    albums = false;
                }
                if (section.more == false) {
                    loadMore.addClass('noMore');
                    loadMore.html(section.output);
                    loadMore.removeClass('load-chosen');
                } else {
                    $('#section-content').append(section.output);
                    var newOffset = parseInt(offset) + 6;
                    loadMore.attr('offset', newOffset);
                }
                if (videos) {
                    var videosUL = $("#videos");
                    videosUL.html('');
                    if (videos.output !== '') {
                        videosUL.html(videos.output);
                    }
                }
                if (albums) {
                    var albumsContainer = $("#albums");
                    var albumContent = $('#album-container');
                    albumsContainer.html('');
                    albumContent.html('').hide();
                    if (albums.output !== '') {
                        albumsContainer.html(albums.output);
                    }
                }
                Highlight();
            },
            complete: function() {
                loadMore.show();
                self.parent('li').addClass('active');
                if (loadMore.hasClass('noMore')) {
//                    loadMore.hide('slow');
//                    loadMore.removeClass('load-chosen');
                } else {
//                    var newOffset = parseInt(offset) + 2;
//                    loadMore.attr('offset', newOffset);
                }
            }
        });
    });
    $('a.fetch-album').live('click', function(event) {
        event.preventDefault();
        self = $(this);
        var postId = self.attr('pid');
        $.ajax({
            url: phpObj.ajaxPath,
            dataType: 'json',
            data: {
                action: 'loadAlbum',
                post: postId
            },
            beforeSend: function() {
                $('.album-act.active').removeClass('active');
            },
            success: function(res) {
                var container = $('#album-container');
                container.html('');
                $('<div>', {class: 'mygallery'}).html($('<div>', {class: 'tn3 album'}).html($('<h4>').html('Cap').appendTo(container))).appendTo(container);
//                $('<div>', {class: 'tn3 album'}).appendTo(container);
                var tn3Con = $('.tn3.album');
                $('<h4>').html('Cap').appendTo(tn3Con);
                $('<div>', {class: 'tn3 description'}).html('decs').appendTo(tn3Con);
                $('<div>', {class: 'tn3 thumb'}).html('images/35x35/1.jpg').appendTo(tn3Con);
                var tempCon = $('<ol>').appendTo(tn3Con);
                $.each(res, function(key, value) {
                    var caption = value.caption;
                    var source = getOriginalFromThumb(value.src);
                    var li = $('<li>').html($('<h4>').html(caption)).appendTo(tempCon);
                    $('<a>', {
                        href: source
                    }).html($('<img>', {
                        src: source
                    })).appendTo(li);
                });
                var tn1 = $('.mygallery').tn3({
                    skinDir: phpObj.templateDir + "/images",
                    imageClick: "fullscreen",
                    image: {
                        maxZoom: 1.5,
                        crop: true,
                        clickEvent: "dblclick",
                        transitions: [{
                                type: "blinds"
                            }, {
                                type: "grid"
                            }, {
                                type: "grid",
                                duration: 460,
                                easing: "easeInQuad",
                                gridX: 1,
                                gridY: 8,
                                // flat, diagonal, circle, random
                                sort: "random",
                                sortReverse: false,
                                diagonalStart: "bl",
                                // fade, scale
                                method: "scale",
                                partDuration: 360,
                                partEasing: "easeOutSine",
                                partDirection: "left"
                            }]
                    }
                });
                $('div.photoAlbums').show('slow');
            },
            complete: function() {

                self.parent('li').addClass('active');
            }
        });
    });
    if ($('#load-chosen-all').length > 0) {
        $('#load-chosen-all').trigger('click');
    }
    if ($('#first-album').length > 0) {
        console.log('yes');
        $('#first-album').trigger('click');
    }
    if ($(".sectionOneNewBox").length) {
        Highlight();
    }
    if ($('.mygallery').length > 0) {
        var tn1 = $('.mygallery').tn3({
            skinDir: phpObj.templateDir + "/images",
            imageClick: "fullscreen",
            image: {
                maxZoom: 1.5,
                crop: true,
                clickEvent: "dblclick",
                transitions: [{
                        type: "blinds"
                    }, {
                        type: "grid"
                    }, {
                        type: "grid",
                        duration: 460,
                        easing: "easeInQuad",
                        gridX: 1,
                        gridY: 8,
                        // flat, diagonal, circle, random
                        sort: "random",
                        sortReverse: false,
                        diagonalStart: "bl",
                        // fade, scale
                        method: "scale",
                        partDuration: 360,
                        partEasing: "easeOutSine",
                        partDirection: "left"
                    }]
            }
        });
    }

    //map
    $('path').live('click', function() {
        var self = $(this);
        if (self.attr('disabledAjax') === 'true') {

        } else {
            $('path').attr('disabledAjax', 'false');
            var tooltipContainer = $('#tooltipMap');
            var ttNews = tooltipContainer.find('.ttnews');
            var ttTitle = tooltipContainer.find('.titleTTM');
            ttNews.html('');
            ttTitle.html('');
            var province = self.attr('id');
            if (province && typeof province !== 'undefined') {
                $.ajax({
                    url: phpObj.ajaxPath,
                    dataType: 'json',
                    data: {
                        action: 'loadProvinceNews',
                        province: province
                    },
                    beforeSend: function() {
                        self.attr('disabledAjax', 'true');
                    },
                    success: function(res) {
                        var posts = res.posts;
                        ttTitle.html(res.title);
                        if (posts.length > 0) {
                            $.each(posts, function(key, value) {
                                $('<a>', {
                                    'href': value.link
                                }).html(value.title).appendTo(ttNews);
                            });
                        } else {
                            $('<a>', {
                                'href': '#'
                            }).html('لا يوجد اخبار').appendTo(ttNews);
                        }
                    },
                    complete: function() {
                        $("#cur-prov").val(province);
                    }
                });
            }
        }
    });
    $('.navGoSection').live('click', function(event) {
        var self = $(this);
        var province = $("#cur-prov").val();
        if (province && typeof province !== 'undefined') {
            $('#tooltipMap').fadeOut('fast');
            $('#province-name').html(province);
            $("html, body").animate({scrollTop: $('#province-name').offset().top - 75}, 1000);
            $('#load-chosen-all').trigger('click');
        }
    });
});